{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a90433e-f5a3-4490-80a6-940379b7a22f",
   "metadata": {},
   "source": [
    "# 10. 卷积神经网络\n",
    "\n",
    "- 单通道卷积\n",
    "- 多通道卷积\n",
    "- 全零填充\n",
    "- 批标准化\n",
    "- 池化\n",
    "- 舍弃\n",
    "- 卷积神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 10.1 实现单通道图像卷积计算\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设一张图像的大小为5（行）×5（列）×1（通道），其像素矩阵如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "5595ce7a-1649-466f-92af-5d9c166c1aae",
   "metadata": {},
   "source": [
    "[[2,1,0,2,3],\n",
    "[9,5,4,2,0],\n",
    "[2,3,4,5,6],\n",
    "[1,2,3,1,0],\n",
    "[0,4,4,2,8]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d884165-5699-4cb9-9896-3686ae70345c",
   "metadata": {},
   "source": [
    "有一个3×3×1的卷积核，其像素矩阵如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "61b6e228-60be-4bea-98d0-bb589c33793a",
   "metadata": {},
   "source": [
    "[[-1,0,1],\n",
    "[-1,0,1],\n",
    "[-1,0,1]]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55043130-4496-43a3-803b-9bc1cea8b1b8",
   "metadata": {},
   "source": [
    "### 3.任务分析构\n",
    "\n",
    "对于二维图像的卷积计算，可以使用tf.nn.conv2d方法。\r\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入形状: (1, 5, 5, 1)\n",
      "卷积核形状: (3, 3, 1, 1)\n",
      "输出形状: (1, 3, 3, 1)\n",
      "输出:\n",
      " [[-4.  1.  2.]\n",
      " [ 0. -1. -4.]\n",
      " [ 9.  0.  4.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# 1，定义输入\n",
    "# 原始输入\n",
    "input=tf.constant([[2,1,0,2,3],\n",
    "                       [9,5,4,2,0],\n",
    "                       [2,3,4,5,6],\n",
    "                       [1,2,3,1,0],\n",
    "                       [0,4,4,2,8]],dtype=tf.float32)\n",
    "# 进行形状转换（增加批维度）\n",
    "input=tf.expand_dims(input,0)\n",
    "# 进行形状转换（增加通道维度）\n",
    "input=tf.expand_dims(input,3)\n",
    "print(\"输入形状:\",input.shape)\n",
    "# 2，定义卷积核\n",
    "# 维度格式：[filter_height, filter_width, in_channels, out_channels] \n",
    "filters=tf.constant([[-1,0,1],\n",
    "                        [-1,0,1],\n",
    "                        [-1,0,1]],dtype=tf.float32)\n",
    "# 进行形状转换（增加输入通道维度）\n",
    "filters=tf.expand_dims(filters,2)\n",
    "# 进行形状转换（增加输出通道维度）\n",
    "filters=tf.expand_dims(filters,3)\n",
    "print(\"卷积核形状:\",filters.shape)\n",
    "\n",
    "# 3，卷积计算\n",
    "out=tf.nn.conv2d(   \n",
    "    # 输入\n",
    "    input=input,   \n",
    "    # 过滤器（卷积核）\n",
    "    filters=filters,\n",
    "    # 滑动步长\n",
    "    strides=1,\n",
    "    # 填充方式\n",
    "    padding='VALID',\n",
    "    # 指定输入和输出数据的数据格式\n",
    "    data_format='NHWC')\n",
    "\n",
    "# 4，加偏置项\n",
    "b=tf.constant(1,dtype=tf.float32)\n",
    "out=out+b\n",
    "print(\"输出形状:\",out.shape)\n",
    "# 删除长度为1的维度\n",
    "out=tf.squeeze(out)\n",
    "print(\"输出:\\n\",out.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
